layui.use(['form', 'layedit', 'laydate','jquery'], function() {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate
        , $ = layui.$;
    //生日
    laydate.render({
        elem: '#userBirthday'
    });

    //创建一个编辑器
    //var editIndex = layedit.build('userRemark');

    //监听提交
    form.on('submit(tijiao)', function(data){
        // layer.alert(JSON.stringify(data.field), {
        //     title: '最终的提交信息'
        // })

        //利用ajax向后台提交数据
        let url = "/BookUserServlet?action=addOrUpdate";
        let data_ = data.field;
        let spCodesTemp = "";
        //取到所有被选中的复选框
        $('input:checkbox[name=userHobby]:checked').each(function(i){
            if(0==i){
                spCodesTemp = $(this).val();
            }else{
                spCodesTemp += (","+$(this).val());
            }
        });
        data_.userHobby=spCodesTemp;
        console.info(data_);

        $.post(url,data_,function (resp) {
            //layer.msg(resp.msg);

            if(resp.state==1){
                //注意：parent 是 JS 自带的全局对象，可用于操作父页面
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
                //父页面刷新
                parent.table.reload('dataListTable');

                //父页面提示信息
                parent.layer.msg(resp.msg);

            }else{
                layer.msg(resp.msg);
            }
        },"json");

        return false;
    });
    
    $("#close_btn").on("click",function () {
        layerClose();
    });

    /*
    * 当点击编辑按钮时,会传入一个id到form.html页面
    * */
    let id = getQueryString("id");
    //如果id有值
    if(id){
        let url = "/BookUserServlet?action=showUpdate";
        let data_ = {id:id};
        $.post(url,data_,function (resp) {
            if(resp.state==1){
                //$("#userId").val(resp.data.userId);
                //$("#userName").val(resp.data.userName);

                form.val('example', resp.data);

                //处理复选框的问题
                $("input:checkbox[name='userHobby']").prop("checked",false);

                let arrs = resp.data.userHobby.split(",");
                for(let i=0;i<arrs.length;i++){
                    $("input:checkbox[value='"+arrs[i]+"']").prop("checked",true);
                }
                //重新渲染
                form.render();

            }
        },"json");
    }


});


